//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import com.moorwind.gFeed.data.GFindResponse;
	
	import flash.events.Event;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.controller.SearchFeedCommand;
	import reader.core.IGCAble;
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.events.BlogSourceEvent;
	import reader.events.SearchFeedEvent;
	import reader.events.ServiceEvent;
	import reader.model.IDataProxy;
	import reader.model.IServiceProxy;
	import reader.model.vo.BlogSourceVO;
	import reader.services.ISubscribeAble;
	import reader.services.ServiceFactory;
	import reader.view.components.AddSubscribePanel;

	public class AddSubscribeMediator extends ServiceAbleMediator implements IMediator, IGCAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		
		public static const NAME:String = "AddSubscribeMediator";
		
		public function get addSubscribePanel():AddSubscribePanel
		{
			return this.viewComponent as AddSubscribePanel;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var dataProxy:IDataProxy;
		private var serviceProxy:IServiceProxy;
		private var subscribeService:ISubscribeAble;
		
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function AddSubscribeMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			
			startUp();
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		override public function startUp():void
		{
			facade.registerCommand(ApplicationFacade.SEARCH_FEED, SearchFeedCommand);
			dataProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.dataProxyName) as IDataProxy;
			serviceProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.serviceProxyName) as IServiceProxy;
			addSubscribePanel.addEventListener(BlogSourceEvent.SOURCE_CREATE, onSubScribeLoad_handler, false, 0, true);
			addSubscribePanel.addEventListener(BlogSourceEvent.FRIENDS_SUBSCRIBE, onFriendsSubscribe_handler, false, 0, true);			
			addSubscribePanel.addEventListener(ServiceEvent.SUBSCRIBE_SERVICE, onSubscribe_handler, false, 0, true);
			addSubscribePanel.addEventListener(AddSubscribePanel.CLOSE_PANEL, onClosePanle_handler, false, 0, true);
			addSubscribePanel.addEventListener(SearchFeedEvent.SEARCH_FEED, onSourceSearch_handler, false, 0, true);
		}
		
		override public function clearReference():void
		{
			facade.removeCommand(ApplicationFacade.SEARCH_FEED);
			facade.removeMediator(NAME);
			addSubscribePanel.removeEventListener(BlogSourceEvent.SOURCE_CREATE, onSubScribeLoad_handler);
			addSubscribePanel.removeEventListener(BlogSourceEvent.FRIENDS_SUBSCRIBE, onFriendsSubscribe_handler);
			addSubscribePanel.removeEventListener(SearchFeedEvent.SEARCH_FEED, onSourceSearch_handler);
			addSubscribePanel.removeEventListener(ServiceEvent.SUBSCRIBE_SERVICE, onSubscribe_handler);
			addSubscribePanel.removeEventListener(AddSubscribePanel.CLOSE_PANEL, onClosePanle_handler);
		}

		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.SERVICE_SUBSCRIBES, ApplicationFacade.FEEDS_SEARCHED]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.SERVICE_SUBSCRIBES:
					var list:Vector.<BlogSourceVO> = notification.getBody() as Vector.<BlogSourceVO>;
					addSubscribePanel.subscribeList.dataProvider = BlogSourceVO.VectorToArray(list);
					break;
				case ApplicationFacade.FEEDS_SEARCHED:
					var gf:GFindResponse = notification.getBody() as GFindResponse;
					if(gf) 
					{
						addSubscribePanel.closeWindow();
						sendNotification(ApplicationFacade.SEARCH_SUBSCRIBE, gf);
					}
					else
					{
						Alert.show(ReaderResource.instance.resource.addSubscribePanelSearchErrorTip, "", 4, readerModelLocator.airReader);
					}
					break;				
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//events
		
		private function onClosePanle_handler(e:Event):void
		{
			clearReference();
		}
		
		private function onFriendsSubscribe_handler(e:BlogSourceEvent):void
		{
			dataProxy.addListSubscribe(e.listSource, e.label);
		}
		
		/**
		 * add a new sunscribe. 
		*/
		private function onSubScribeLoad_handler(e:BlogSourceEvent):void
		{
			dataProxy.addSubscribe(e.blogSource);
		}
		
		/**
		 * search feed by the input text. 
		*/
		private function onSourceSearch_handler(e:SearchFeedEvent):void
		{
			sendNotification(ApplicationFacade.SEARCH_FEED, e.keyword);
		} 
		
		private function onSubscribe_handler(e:ServiceEvent):void
		{
			subscribeService = ServiceFactory.getSubscribeService(e.service, serviceProxy, addSubscribePanel, servicePool);
			subscribeService.addSubscribe();
		}
	}
}